package com.itheima.web;

import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addUserServlet")
public class AddUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 本次请求携带的是json格式的数据,需要单独解析
            // 获取请求携带的参数信息
            User user = BaseController.jsonStrToJavaObject(request, User.class);
            System.out.println("请求携带的参数为: "+user);
            // 调用service处理业务逻辑
            UserService service = new UserService();
            service.addUser(user);
            Result result = new Result(true, "添加成功", null);
            BaseController.responseJson(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false, "服务器忙", null);
            BaseController.responseJson(response,result);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}